﻿#include "mytableitemdelegate.h"
#include <QDateTimeEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QListWidget>
#include <QListWidgetItem>

#include "MenuInfo.h"
#include "CheckComboBox.h"
#include <QtGlobal>
#include <QDateTime>
#include <QLineEdit>
#include <QColor>
#include <QColorDialog>
#include "relatedlg.h"
#include "IcdRelatedlg.h"
#include <QIntValidator>
#include <QDoubleValidator>
#include <QRegExp>
#include <QRegExpValidator>
//#include "NodeCfgDlg.h" 
//#include "ChoiceIndexNoDlg.h"
#include "ExpEditDlg.h"
//#include "SGZYDlg.h"
#include <QChar>
//#include "GlobObjectDlg.h"

MyTableItemDelegate::MyTableItemDelegate(MyTableData *pTableData,/*int TableType,*/ QWidget *parent) : QItemDelegate(parent)
{
	m_pTableData = NULL;
	m_pMenuInfo = NULL;
	m_pTableData = pTableData;
	m_pTableData->GetMenuInfoPoint(m_pMenuInfo);
	//m_TableType=TableType;
}

MyTableItemDelegate::~MyTableItemDelegate()
{

}



// 当双击表格里的某一个元素时，会调用这个函数，创造一个Editor
QWidget *MyTableItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	if (!m_pTableData)
	{
		return QItemDelegate::createEditor(parent, option, index);
	}

	if (!m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::createEditor(parent, option, index);
	}
	int	FieldNo = index.column();
	int	RowNo = index.row();
	short	DisplayType = 0;
	int	ColumnId = 0;
	if (!m_pTableData->IsRelateColumnEnableEdit(RowNo, FieldNo))
	{
		return NULL;
	}
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	ColumnId = m_pTableData->GetFieldColumnId(FieldNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
		{
			QComboBox *editor = new QComboBox(parent);
			int	Index = 0;
			QString ItemText ;
			ItemText = QString::fromLocal8Bit("否");
			int	UserData = SYS_STATE_INVALID;
			editor->insertItem(Index++, ItemText, QVariant(UserData));
			ItemText = QString::fromLocal8Bit("是");
			UserData = SYS_STATE_VALID;
			editor->insertItem(Index, ItemText, QVariant(UserData));

			int i = editor->findData(index.model()->data(index, Qt::UserRole).toInt());
			editor->setCurrentIndex(i);
			return editor;
		}
		break;
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *MenuEditor = new QComboBox(parent);
			QString ItemText;
			int	UserData = 0;
			int	UserDataBefore = 0;
			int	MenuNum = 0;
			ItemMenu *pMenuInfo = NULL;
			Field_Menu FieldMenuInfoTemp;
			cColumn_Info column_info_temp;
			m_pTableData->GetFieldColumnInfo(FieldNo, column_info_temp);
			if (m_pMenuInfo)
			{
				if (QString::fromLocal8Bit(column_info_temp.ColumnEngName)=="m_ProtType")
				{
					UserDataBefore=index.model()->data(index.model()->index(RowNo,FieldNo-1),Qt::UserRole).toInt();
					QString MenuName=m_pMenuInfo->GetMenuNameByTaskType(UserDataBefore);
					m_pMenuInfo->GetMenuInfo(FieldMenuInfoTemp,MenuName.toLocal8Bit().data());
					pMenuInfo=FieldMenuInfoTemp.pMenu;
					MenuNum=FieldMenuInfoTemp.menuNum;
				}
				//ccg如果列的英文名是逻辑设备（ParentId），不走正常的获取menuinfo流程，自己根据设备名造下拉列表
				/*else if (QString::fromLocal8Bit(column_info_temp.ColumnEngName)=="ParentId")
				{
					//读LogicDevice表,返回表的Name和ID;
					int LogicDeviceNum =0;
					char	*buff = NULL;
					int	buff_len = 0;
					int	TableRecordLen =0;
					LogicDeviceNum = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RLOGIC_DEVICE,buff, &buff_len, &TableRecordLen,true);
					int a = 0;	
					//根据设备的个数循环，添加comboBox并设定下拉框的值；
					for (int i =0;i<LogicDeviceNum;i++)
					{
						ItemText = QString::fromLocal8Bit(((rLogic_Device*)buff)[i].Name);
						UserData = ((rLogic_Device*)buff)[i].ID;
						MenuEditor->insertItem(i, ItemText, QVariant(UserData));
					}
					int CurIndex = MenuEditor->findData(index.model()->data(index, Qt::UserRole).toInt());
					if (CurIndex>= 0 && CurIndex<LogicDeviceNum)
					{
						MenuEditor->setCurrentIndex(CurIndex);
					}else
					{
						MenuEditor->setCurrentIndex(0);
					}
					return MenuEditor;
				}*/
				else
				{
					MenuNum = m_pMenuInfo->GetMenu(ColumnId, pMenuInfo);//ccg获取下拉框个数、下拉列表详细信息
				}
				if (MenuNum>0 && pMenuInfo)
				{
					for (int i=0; i<MenuNum; i++)
					{
						if (pMenuInfo[i].pDisplayText)
						{
							ItemText = QString::fromLocal8Bit(pMenuInfo[i].pDisplayText);
						}
						UserData = pMenuInfo[i].ActVal;
						MenuEditor->insertItem(i, ItemText, QVariant(UserData));
					}
					int CurIndex = MenuEditor->findData(index.model()->data(index, Qt::UserRole).toInt());
					if (CurIndex>= 0 && CurIndex<MenuNum)
					{
						MenuEditor->setCurrentIndex(CurIndex);
					}else
					{
						MenuEditor->setCurrentIndex(0);
					}
					return MenuEditor;
				}
			}

			return QItemDelegate::createEditor(parent, option, index);
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = new CheckComboBox(parent);
			QString			ItemText;
			int			UserData = 0;
			int			MenuNum = 0;
			ItemMenu		*pMenuInfo = NULL;
			int			ItemData = index.model()->data(index, Qt::UserRole).toInt();
			if (m_pMenuInfo)
			{
				MenuNum = m_pMenuInfo->GetMenu(ColumnId, pMenuInfo);
				if (MenuNum>0 && pMenuInfo)
				{
					MultiMenuEditor->setMaxCount(MenuNum);
					for (int i=0; i<MenuNum; i++)
					{
						if (pMenuInfo[i].pDisplayText)
						{
							ItemText = QString::fromLocal8Bit(pMenuInfo[i].pDisplayText);
						}
						UserData = pMenuInfo[i].ActVal;
						MultiMenuEditor->addItemText(ItemText, QVariant(UserData), false);
						if ((UserData&ItemData)>0)
						{
							MultiMenuEditor->setCheck(i, true);
						}else
						{
							MultiMenuEditor->setCheck(i, false);
						}
					}
					return MultiMenuEditor;
				}
			}

			return QItemDelegate::createEditor(parent, option, index);
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *editor = new QTimeEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("hh:mm:ss");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			QDateEdit *editor = new QDateEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("yyyy-M-dd");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *editor = new QDateTimeEdit(parent);   //显示日期的控件
			editor->setDisplayFormat("yyyy-M-ddThh:mm:ss");
			editor->setCalendarPopup(true);
			return editor;
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			if (m_pTableData->IsNewDataUnSaved())
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("有未保存的修改内容，请先保存再进行关联组态，防止修改内容丢失！"));
				return NULL;
			}
			// 			if (!m_pTableData->IsRelateColumnEnableEdit(RowNo, FieldNo))
			// 			{
			// 				return NULL;
			// 			}

			unsigned int	OldItemData = index.model()->data(index, Qt::UserRole).toUInt();
			unsigned int	CurRecordId = 0;
			m_pTableData->GetCurRecordIdbyRowIndex(RowNo, CurRecordId);
			cColumn_Info ColumnInfo;
			if (m_pTableData->GetFieldColumnInfo(FieldNo, ColumnInfo)>0)
			{
				//ccg在此加分支，如果是61850模型配置，调用IcdRelateDlg对话框
				if(m_pTableData->GetCurTableId()==TABLE_RDATA_OBJECT)
				{
					IcdRelatedlg *IcddlgEditor = new IcdRelatedlg(&ColumnInfo, OldItemData, CurRecordId/*, parent*/);
					return IcddlgEditor;
				}else
				{
					RelateDlg *dlgEditor = new RelateDlg(&ColumnInfo, OldItemData, CurRecordId/*, parent*/);
					return dlgEditor;
				}
			}
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = new QColorDialog();
			return pColorDlg;
		}	
		break;
		// 	case DISPLAY_TYPE_TABLE:
		// 		{
		// 
		// 		}
		// 		break;
		// 	case DISPLAY_TYPE_COLUMN:
		// 		{
		// 
		// 		}
		// 		break;
	case DISPLAY_TYPE_CHAR:
		{
			QValidator *validator = new QIntValidator(-0x80, 0x7F, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_UCHAR:
		{
			QValidator *validator = new QIntValidator(0x0, 0xFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_SHORT:
		{
			QValidator *validator = new QIntValidator(-0x8000, 0x7FFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_USHORT:
		{
			QValidator *validator = new QIntValidator(0x0, 0xFFFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_INT32:
		{
			QValidator *validator = new QIntValidator(-0x80000000, 0x7FFFFFFF, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_UINT32:  //6
		{
			QRegExp rx("\\d{0,10}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_LONGLONG:
		{
			QRegExp rx("[-0-9]\\d{0,20}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_ULONGLONG:
		{
			QRegExp rx("\\d{0,20}");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_FLOAT:
	case DISPLAY_TYPE_DOUBLE:
		{
			QValidator *validator = new QDoubleValidator(parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_EXP:
		{
			if (m_pTableData->IsNewDataUnSaved())
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("有未保存的修改内容，请先保存再进行表达式组态，防止修改内容丢失！"));
				return NULL;
			}
			unsigned int	CurRecordId = 0;
			m_pTableData->GetCurRecordIdbyRowIndex(RowNo, CurRecordId);
			ExpEditDlg *pExpDlg = new ExpEditDlg(CurRecordId);
			return pExpDlg;
			
		}
		break;
	case DISPLAY_TYPE_INDEXNO:
		{
			if (m_pTableData->IsNewDataUnSaved())
			{
				QMessageBox::information(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("有未保存的修改内容，请先保存，防止修改内容丢失！"));
				return NULL;
			}
			/*int AppType = m_pTableData->GetCurAppType();
			int TableId = m_pTableData->GetCurTableId();
			unsigned int	CurRecordId = 0;
			m_pTableData->GetCurRecordIdbyRowIndex(RowNo, CurRecordId);
			ChoiceIndexNoDlg *pChoiceIndexDlg = new ChoiceIndexNoDlg(AppType, TableId, CurRecordId, ColumnId);
			return pChoiceIndexDlg;*/
			QLineEdit *editor = new QLineEdit(parent); 
			return editor;
		}
		break;

	case DISPLAY_TYPE_HEX:
		{
			QRegExp rx("[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]");
			QValidator *validator = new QRegExpValidator(rx, parent);
			QLineEdit *lineEdit = new QLineEdit(parent);
			lineEdit->setValidator(validator);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_IP_CFG:
		{
			QLineEdit *lineEdit = new QLineEdit(parent);
			return lineEdit;
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_PASSWORD:
	default:
		{
			return QItemDelegate::createEditor(parent, option, index);

		}
		break;
	}
	return QItemDelegate::createEditor(parent, option, index);
}

void MyTableItemDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const
{
	//qDebug() << "setEditorData ItemData" << QDateTime::currentDateTime();
	if (!m_pTableData)
	{
		return QItemDelegate::setEditorData(editor, index);
	}

	if (!m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::setEditorData(editor, index);
	}
	int	ColumnNo = index.column();
	short	DisplayType = 0;
	DisplayType = m_pTableData->GetFieldDisplayType(ColumnNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
			if(comboBox)
			{
				int i = comboBox->findData(index.model()->data(index, Qt::UserRole).toInt());
				comboBox->setCurrentIndex(i);
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = qobject_cast<CheckComboBox*>(editor);
			if (MultiMenuEditor)
			{
				int ItemData = index.model()->data(index, Qt::UserRole).toInt();
				int DataBitNum = 8*sizeof(int);
				int ItemNum = MultiMenuEditor->maxCount();
				int bitVal = 0;
				DataBitNum = qMin(DataBitNum, ItemNum);
				for (int i=0; i<DataBitNum ; i++)
				{
					bitVal = MultiMenuEditor->itemData(i).toInt();
					if ((bitVal&ItemData)>0)
					{
						MultiMenuEditor->setCheck(i, true);
					}else
					{
						MultiMenuEditor->setCheck(i, false);
					}
				}
			}
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *dateEditor = qobject_cast<QTimeEdit *>(editor);
			if (dateEditor) 
			{
				dateEditor->setTime(QTime::fromString(index.model()->data(index, Qt::UserRole).toString(), "hh:mm:ss"));
			}
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			QDateEdit *dateEditor = qobject_cast<QDateEdit *>(editor);
			if (dateEditor) 
			{
				dateEditor->setDate(QDate::fromString(index.model()->data(index, Qt::UserRole).toString(), "yyyy-M-d"));
			}
		}
		break;
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
			if (dateEditor) 
			{
				QDateTime dateTime ;
				dateTime.setTime_t(index.model()->data(index, Qt::UserRole).toUInt());
				dateEditor->setDateTime(dateTime);
				//dateEditor->setDateTime(QDate::fromString(index.model()->data(index, Qt::EditRole).toString(), "yyyy/M/d"));
			}
		}
		break;
		// 	case DISPLAY_TYPE_RELATED:
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = NULL;
			pColorDlg = qobject_cast<QColorDialog *>(editor);
			if (pColorDlg) 
			{
				pColorDlg->setCurrentColor(QColor(QRgb(index.model()->data(index, Qt::UserRole).toUInt())));
			}
		}
		break;
		// 	case DISPLAY_TYPE_TABLE:
		// 		{
		// 
		// 		}
		// 		break;
		// 	case DISPLAY_TYPE_COLUMN:
		// 		{
		// 
		// 		}
		// 		break;
		// 	case DISPLAY_TYPE_STRING:
		// 		{
		// 		}
		// 		break;
	case DISPLAY_TYPE_CHAR:
	case DISPLAY_TYPE_UCHAR:
	case DISPLAY_TYPE_SHORT:
	case DISPLAY_TYPE_USHORT:
	case DISPLAY_TYPE_INT32:
	case DISPLAY_TYPE_UINT32:
	case DISPLAY_TYPE_LONGLONG:
	case DISPLAY_TYPE_ULONGLONG:
	case DISPLAY_TYPE_FLOAT:
	case DISPLAY_TYPE_DOUBLE:
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_HEX:
	case DISPLAY_TYPE_PASSWORD:
		//case DISPLAY_TYPE_BINARY:
	default:
		{
			return QItemDelegate::setEditorData(editor, index);
		}
		break;
	}
}

void MyTableItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const
{
	if (!index.isValid() || !model || !editor)
	{
		return;
	}
	if (!m_pTableData || !m_pTableData->IsRecordParamSafe())
	{
		return QItemDelegate::setModelData(editor, model, index);
	}

	int		FieldNo = index.column();
	int		rowNo   = index.row();
	int     RecordNo = m_pTableData->GetCurRecordNoByRowNo(rowNo); 
	int		sibRowNo = 0;
	int		ColumnId = 0;
	short		DisplayType = 0;

	bool		bRefresh = false;
	QVariant	OldUserData = model->data(index, Qt::UserRole);
	QVariant	NewUserData;

	int		RecId1=0;
	int		RecId2=0;
	int		RecId3=0;

	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	ColumnId = m_pTableData->GetFieldColumnId(FieldNo);

	switch (DisplayType)
	{
	case DISPLAY_TYPE_BOOLEN:
	case DISPLAY_TYPE_MENU:
		{
			QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
			if(comboBox)
			{
				int ItemData = comboBox->itemData(comboBox->currentIndex()).toInt();				
				model->setData(index, comboBox->currentText(), Qt::DisplayRole);
				model->setData(index, ItemData, Qt::UserRole);

				int Ret = m_pTableData->GetSibRecord(rowNo, sibRowNo, FieldNo);
				if (Ret>0 && sibRowNo>=0)
				{
					SetSibRecordColumn(model, index, sibRowNo, FieldNo, comboBox->currentText(), ItemData);
				}
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
		{
			CheckComboBox	*MultiMenuEditor = qobject_cast<CheckComboBox*>(editor);
			if (MultiMenuEditor)
			{
				int DataBitNum = 8*sizeof(int);
				int ItemNum = MultiMenuEditor->maxCount();
				int ItemData = 0;
				DataBitNum = qMin(DataBitNum, ItemNum);
				for (int i=0; i<DataBitNum ; i++)
				{
					if (MultiMenuEditor->isChecked(i))
					{	
						ItemData += MultiMenuEditor->itemData(i).toInt();
					}
				}

				//if (m_pTableData->CheckUpDateColumnIsSafe(rowNo, FieldNo, (XCHAR*)&ItemData))
				//{
				model->setData(index, MultiMenuEditor->getSelString(), Qt::DisplayRole);
				model->setData(index, ItemData, Qt::UserRole);

				int Ret = m_pTableData->GetSibRecord(rowNo, sibRowNo, FieldNo);
				if (Ret>0 && sibRowNo>=0)
				{
					SetSibRecordColumn(model, index, sibRowNo, FieldNo, MultiMenuEditor->getSelString(), ItemData);
				}
				//}
			}
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			QTimeEdit *dateEditor = qobject_cast<QTimeEdit *>(editor);
			if (dateEditor) 
			{
				model->setData(index, dateEditor->time().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, dateEditor->time().toString(Qt::ISODate), Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			QDateEdit *dateEditor = qobject_cast<QDateEdit *>(editor);
			if (dateEditor) 
			{
				model->setData(index, dateEditor->date().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, dateEditor->date().toString(Qt::ISODate), Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTimeEdit *dateEditor = qobject_cast<QDateTimeEdit *>(editor);
			if (dateEditor) 
			{
				unsigned int Result = dateEditor->dateTime().toTime_t();
				model->setData(index, dateEditor->dateTime().toString(Qt::ISODate), Qt::DisplayRole);
				model->setData(index, Result, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			QString ItemText;
			unsigned int	NewItemData = 0;
			//ccg增加引用类型分支61850模型配置
			if(m_pTableData->GetCurTableId()==TABLE_RDATA_OBJECT)
			{
				IcdRelatedlg *icddlgEditor = qobject_cast<IcdRelatedlg *>(editor);
				if (icddlgEditor) 
				{
					if (icddlgEditor->GetRelateData(NewItemData, ItemText)>0)
					{
						icddlgEditor->GetRecordId(RecId1,RecId2,RecId3);
						model->setData(index, ItemText, Qt::DisplayRole);
						model->setData(index, NewItemData, Qt::UserRole);
						//	bRefresh = true;
					}
				}
			}
			else
			{
				RelateDlg *dlgEditor = qobject_cast< RelateDlg *>(editor);
				if (dlgEditor) 
				{
					if (dlgEditor->GetRelateData(NewItemData, ItemText)>0)
					{
						dlgEditor->GetRecordId(RecId1,RecId2,RecId3);
						model->setData(index, ItemText, Qt::DisplayRole);
						model->setData(index, NewItemData, Qt::UserRole);
						//	bRefresh = true;
					}
				}

			}
			
			
			
		
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColorDialog *pColorDlg = NULL;
			unsigned int	NewItemData = 0;
			QString ItemText;
			QColor curColor;
			pColorDlg = qobject_cast<QColorDialog *>(editor);
			if (pColorDlg) 
			{
				curColor = pColorDlg->currentColor();
				NewItemData = curColor.rgba();
				ItemText = QString("(%1, %2, %3 ,%4)").arg(curColor.red()).arg(curColor.green()).arg(curColor.blue()).arg(curColor.alpha());
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, curColor, Qt::DecorationRole);
				//model->setData(index, curColor, Qt::BackgroundRole);
				model->setData(index, NewItemData, Qt::UserRole);
			}
		}		
		break;
		// 	case DISPLAY_TYPE_TABLE:
		// 		{
		// 
		// 		}
		// 		break;
		// 	case DISPLAY_TYPE_COLUMN:
		// 		{
		// 
		// 		}
		// 		break;
	case DISPLAY_TYPE_CHAR:
		{
			QString ItemText;
			char	CharVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				CharVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, CharVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_UCHAR:
		{
			QString ItemText;
			unsigned char	uCharVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uCharVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uCharVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_SHORT:
		{
			QString ItemText;
			short	ShortVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				ShortVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, ShortVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_USHORT:
		{
			QString ItemText;
			unsigned short	uShortVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uShortVal = ItemText.toInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uShortVal, Qt::UserRole);
			}
		}
		break;;
	case DISPLAY_TYPE_INT32:
		{
			QString ItemText;
			int	IntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				IntVal = ItemText.toInt();
				int RelatedFieldNo=0;
				if (m_pTableData->GetRelatedFieldNo(FieldNo, RelatedFieldNo)>0)
				{
					QString CodeStr;
					if (m_pTableData->GetNewCodeStr(rowNo, IntVal, CodeStr)>0)
					{
						SetRelateColumnData(model, index, rowNo, RelatedFieldNo, CodeStr, QVariant(CodeStr));
					}
				}
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, IntVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_UINT32:
		{
			QString ItemText;
			unsigned int	uIntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				uIntVal = ItemText.toUInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uIntVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_LONGLONG:
		{
			QString		ItemText;
			qint64	LongLongVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				LongLongVal = ItemText.toLongLong();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, LongLongVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_ULONGLONG:
		{
			QString		ItemText;
			quint64	LongLongVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				LongLongVal = ItemText.toULongLong();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, LongLongVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_FLOAT:
		{
			QString		ItemText;
			float		floatVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				floatVal = ItemText.toFloat();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, floatVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_DOUBLE:
		{
			QString		ItemText;
			double		doubleVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				doubleVal = ItemText.toDouble();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, doubleVal, Qt::UserRole);
			}
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_PASSWORD:
		{
			QString ItemText;
			int	ColumnNo = 0;
			unsigned short	ColDataLen = 0;
			ColumnNo = m_pTableData->GetFieldColumnNo(FieldNo);
			ColDataLen = m_pTableData->GetCurColumnLength(ColumnNo);
			if(m_pTableData->GetCurColumnEngName(ColumnNo)=="WhAddr")
			{
				ColDataLen=ColDataLen-1;
			}
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				ItemText = ItemText.trimmed();
				QByteArray Text = ItemText.toLocal8Bit();
				if (Text.length()>ColDataLen)
				{
					QMessageBox::question(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("输入内容超出字段长度！"));
					model->setData(index, Qt::red, Qt::TextColorRole);
					return ;
				}
				if(m_pTableData->GetCurColumnEngName(ColumnNo)=="WhAddr")
				{
					bool is_ok=false;
					ItemText.toLongLong(&is_ok,10);
					if (!is_ok)
					{
						QMessageBox::question(NULL, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("输入电表地址有误！"));
						model->setData(index, Qt::red, Qt::TextColorRole);
						return ;
					}
				}
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, ItemText, Qt::UserRole);
				qDebug() <<"设置字符串";
			}
		}
		break;
	case DISPLAY_TYPE_EXP:
		{
			ExpEditDlg *pExpDlg = NULL;
			pExpDlg = qobject_cast< ExpEditDlg *>(editor);
			if (pExpDlg)
			{
				bRefresh = true;
			}
		}
		break;
	case DISPLAY_TYPE_INDEXNO:
		{
			/*ChoiceIndexNoDlg *pIndexNoDlg = NULL;
			QString ItemText;	
			pIndexNoDlg = qobject_cast< ChoiceIndexNoDlg *>(editor);
			if (pIndexNoDlg)
			{
				bRefresh = true;
			}*/
			QLineEdit *editor_temp = qobject_cast<QLineEdit *>(editor); 
			if (editor_temp) 
			{
				model->setData(index, editor_temp->text(), Qt::DisplayRole);
				model->setData(index, editor_temp->text(), Qt::UserRole);
			}
			bRefresh = true;
		}
		break;
// 	case DISPLAY_TYPE_INDEXNO_TRACER:
// 		{
// 			QLineEdit *editor_temp = qobject_cast<QLineEdit *>(editor); 
// 			if (editor_temp) 
// 			{
// 				model->setData(index, editor_temp->text(), Qt::DisplayRole);
// 				model->setData(index, editor_temp->text(), Qt::UserRole);
// 			}
// 		}
// 		break;
// 	case DISPLAY_TYPE_NODE_CFG_STATE:
// 		{
// 			/*NodeCfgDlg *pNodeCfgdlg = NULL;
// 			QString ItemText;	
// 			unsigned int	columnId = 0;
// 			columnId = m_pTableData->GetFieldColumnId(FieldNo);
// 			pNodeCfgdlg = qobject_cast< NodeCfgDlg *>(editor);
// 			if (pNodeCfgdlg)
// 			{
// 				unsigned int FieldValue = 0;
// 				pNodeCfgdlg->GetNodeCfgState(FieldValue);
// 				if (m_pMenuInfo)
// 				{
// 					m_pMenuInfo->GetMultiMenuText(columnId,FieldValue, ItemText);
// 
// 					model->setData(index, ItemText, Qt::DisplayRole);
// 					model->setData(index, FieldValue, Qt::UserRole);
// 					int Ret = m_pTableData->GetSibRecord(rowNo, sibRowNo, FieldNo);
// 					if (Ret>0 && sibRowNo>=0)
// 					{
// 						FieldValue = FieldValue&NODE_STATE_TSource_CONN_V;
// 						m_pMenuInfo->GetMultiMenuText(columnId,FieldValue, ItemText);
// 						SetSibRecordColumn(model, index, sibRowNo, FieldNo, ItemText, FieldValue);
// 					}
// 				}
// 			}*/
// 			QLineEdit *editor_temp = qobject_cast<QLineEdit *>(editor); 
// 			if (editor_temp) 
// 			{
// 				model->setData(index, editor_temp->text(), Qt::DisplayRole);
// 				model->setData(index, editor_temp->text(), Qt::UserRole);
// 			}
// 		}
// 		break;
	case DISPLAY_TYPE_HEX:
		{
			QString ItemText;
			unsigned int	uIntVal = 0;
			QChar   NumbChar;
			unsigned char	NumbInt=0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text().toUpper();
				ItemText = ItemText.trimmed();       
				if (ItemText.length()>0)
				{
					for (int i=0; i<4 && i<ItemText.length(); i++)
					{
						NumbChar = ItemText.at(i);
						if (NumbChar.isNumber())
						{
							NumbInt = NumbChar.toAscii()-0x30;
						}else
						{
							if (NumbChar.toAscii()>=65 && NumbChar.toAscii()<=70)
							{
								NumbInt = NumbChar.toAscii()-55;
							}
							else if (NumbChar.toAscii()>=97 && NumbChar.toAscii()<= 102)
							{
								NumbInt = NumbChar.toAscii()-87;
							}
							else
							{
								uIntVal = uIntVal*0x10;
								continue;
							}
						}
						uIntVal = uIntVal*0x10+NumbInt;
					}

					model->setData(index, ItemText, Qt::DisplayRole);
					model->setData(index, uIntVal, Qt::UserRole);
				}
			}
		}
		break;
	case DISPLAY_TYPE_IP_CFG:
		{
			QString			ItemText;
			unsigned int	uIntVal = 0;
			QLineEdit *pLineEditor = qobject_cast< QLineEdit *>(editor);
			if (pLineEditor)
			{
				ItemText = pLineEditor->text();
				QStringList Tmplist = ItemText.split(".");
				QString str;
				quint8 Val=0;
				uIntVal = 0;
				for (int i=0; i<Tmplist.size(); i++)
				{
					Val = Tmplist.at(i).toUInt();
					uIntVal *= 0x100;
					uIntVal += Val;
				}
				//uIntVal = ItemText.toUInt();
				model->setData(index, ItemText, Qt::DisplayRole);
				model->setData(index, uIntVal, Qt::UserRole);
			}
		}
		break;
	default:
		{
			QItemDelegate::setModelData(editor, model, index);
		}
		break;
	}

	NewUserData = model->data(index, Qt::UserRole);
	if (NewUserData != OldUserData && !bRefresh)
	{
		model->setData(index, Qt::green, Qt::TextColorRole);
		m_pTableData->UpDateDataChang(RecordNo, FieldNo,NewUserData);
		emit UserDataChang(RecordNo, FieldNo);
	}
	if (bRefresh)
	{
		// 		if (NewUserData != OldUserData)
		// 		{
		// 			model->setData(index, Qt::magenta, Qt::TextColorRole);
		// 		}
		emit RefreshShow(RecId1, RecId2, RecId3, FieldNo);
	}
	return ;
}
//设置相关记录同一字段内容
void MyTableItemDelegate::SetSibRecordColumn(QAbstractItemModel *model, const QModelIndex &OldIndex, int SibRowNo, int FieldNo, QString ItemText, QVariant ItemData) const
{
	if (!model || !OldIndex.isValid() || SibRowNo <0 || FieldNo<0)
	{
		return ;
	}

	QModelIndex SibIndex;
	SibIndex = OldIndex.sibling(SibRowNo, FieldNo);
	if (SibIndex.isValid())
	{
		model->setData(SibIndex, ItemText, Qt::DisplayRole);
		model->setData(SibIndex, ItemData, Qt::UserRole);
		model->setData(SibIndex, Qt::red, Qt::TextColorRole);
		m_pTableData->UpDateDataChang(SibRowNo, FieldNo, ItemData);
	}
}

void MyTableItemDelegate::SetRelateColumnData(QAbstractItemModel *model, const QModelIndex &OldIndex, int RowNo, int RelateFieldNo, QString ItemText, QVariant ItemData) const
{
	if (!model || !OldIndex.isValid() || RowNo <0 || RelateFieldNo<0)
	{
		return ;
	}

	QModelIndex RelatedIndex;
	RelatedIndex = OldIndex.sibling(RowNo, RelateFieldNo);
	if (RelatedIndex.isValid())
	{
		QVariant OldItemData = model->data(RelatedIndex, Qt::UserRole);
		if (OldItemData != ItemData)
		{
			model->setData(RelatedIndex, ItemText, Qt::DisplayRole);
			model->setData(RelatedIndex, ItemData, Qt::UserRole);
			model->setData(RelatedIndex, Qt::red, Qt::TextColorRole);
			emit UserDataChang(RowNo, RelateFieldNo);
		}
	}
}
void MyTableItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & index )const
{
	if (!editor || !index.isValid())
	{
		return;
	}
	int	FieldNo = index.column();
	short	DisplayType = 0;
	DisplayType = m_pTableData->GetFieldDisplayType(FieldNo);
	if (DISPLAY_TYPE_RELATED != DisplayType &&
/*		DISPLAY_TYPE_NODE_CFG_STATE != DisplayType &&*/
		DISPLAY_TYPE_INDEXNO != DisplayType &&
		DISPLAY_TYPE_EXP != DisplayType )	
	{
		editor->setGeometry(option.rect);
	}
}
